package common;

/**
 * Formats file size into a readable quanity, either B, KB, MB
 *
 */
public class SizeFormatter {
	
	/**
	 * Formats file size into a readable quanity, either B, KB, MB 
	 * or GB depending on file size
	 * @param inData file size in bytes as a String
	 * @return formatted file size as a String
	 */
	public static String fixSizeFormat(String inData) {
		long kb = 1000;
		long mb = 1000000;
		long gb = 1000000000;

		long unformatted = Long.parseLong(inData);
		if (unformatted < kb){
			return inData + " B";
		} else if (unformatted < mb) {
			long left = unformatted / kb;
			long right = (10 * (unformatted % kb)) / (kb);
			return left + "." + right + " KB";
		} else if (unformatted < gb){
			long left = unformatted/mb;
			long right = (10 * (unformatted % mb)) / (mb);
			return left + "." + right + " MB";
		} else {
			long left = unformatted/gb;
			long right = (10 * (unformatted % gb)) / (gb);
			return left + "." + right + " GB";
		} 
	}
}